#! /usr/bin/env python2
# -*- coding: utf-8 -*-

#######################
#	国税重大税收违法案件信息公布栏信息列表 
#######################

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from config import domains,keywords,conn
import pycurl
import time
import StringIO
import urllib
import re
from bs4 import BeautifulSoup

	
def PostData(curl, url, data):
	head = ['Accept-Encoding:gzip,deflate,sdch','Accept-Language:zh-CN,zh;q=0.8','User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0']
	buf = StringIO.StringIO()
	curl.setopt(pycurl.WRITEFUNCTION, buf.write)
	curl.setopt(pycurl.POSTFIELDS,  urllib.urlencode(data))
	curl.setopt(pycurl.URL, url)
	curl.setopt(pycurl.HTTPHEADER,  head)
	curl.perform()
	the_page = buf.getvalue()
	buf.close()
	return the_page

def insertDb(sql):
	cur.execute(sql)
	conn.commit()
	

c = pycurl.Curl()
cur = conn.cursor()
for domain in domains:
	#获取页面内容
	print "获取远程页面。。。"
	html = PostData(c, domain,domains[domain])
	#获取当前页面内容
	soup = BeautifulSoup(html,"html.parser")

	#获取列表数据
	trlist = soup.find_all("table")[2].find_all("tr")
	for tr in trlist[0:10]:

		tdlist = tr.find_all("td",class_="blue14")
		#所属税务机关名称
		
		#纳税人名称

		#纳税人识别号

		#案件性质

		#页面序号（为当前页面生成）
		detailid = re.findall("\d+",tdlist[4].find("input").attrs["onclick"])

		print "获取远程详情页面信息。。。"
		detailhtml = PostData(c, "http://www.bjsat.gov.cn/bjsat/office/jsp/zdsswfaj/wwidquery",{"orgCode":"11100000000","bz":"zh","id":detailid[0]})
		time.sleep(2) # 休眠2秒
		detailsoup = BeautifulSoup(detailhtml,"html.parser")
		#获取列表数据
		detailtrlist = detailsoup.find("tr").find_all("tr")
		#公司名称
		company = detailtrlist[0].find_all("td",class_="blue12_24")[1].text.strip()
		#纳税人识别号
		TaxpayerID = detailtrlist[1].find_all("td",class_="blue12_24")[1].text.strip()
		#组织机构代码
		OrganCode = detailtrlist[2].find_all("td",class_="blue12_24")[1].text.strip()
		#注册地址
		regurl = detailtrlist[3].find_all("td",class_="blue12_24")[1].text.strip()
		#法定代表人
		LegalPerson = detailtrlist[4].find_all("td",class_="blue12_24")[1].text.strip()
		#财务人员
		Accountant = detailtrlist[5].find_all("td",class_="blue12_24")[1].text.strip()
		#中介机构信息
		Agencyinformation = detailtrlist[6].find_all("td",class_="blue12_24")[1].text.strip()
		#案件性质
		casenature = detailtrlist[7].find_all("td",class_="blue12_24")[1].text.strip()
		#主要违法事实
		facts = detailtrlist[8].find_all("td",class_="blue12_24")[1].text.strip()
		#添加时间
		addtime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))

		sql = "insert into dishonesty_nt (company,TaxpayerID,OrganCode,regurl,LegalPerson,Accountant,Agencyinformation,casenature,facts,addtime) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (company,TaxpayerID,OrganCode,regurl,LegalPerson,Accountant,Agencyinformation,casenature,facts,addtime)
		#将结果插入到mysql中,判断是否已经插入信息
		cur.execute("select OrganCode from dishonesty_nt where OrganCode='%s'" % OrganCode)
		isexits = cur.fetchall()
		if not isexits:
			try:
				insertDb(sql)
				print "数据插入成功..."
			except MySQLdb.Error,e:
				print "Mysql Error %d: %s" % (e.args[0], e.args[1])
		else:
			print "数据已存在，未操作..."
	time.sleep(2) # 休眠2秒
